﻿function DrawGeo(view, geoType) {
    require(["esri/symbols/SimpleMarkerSymbol",
        "esri/symbols/SimpleLineSymbol",
        "esri/symbols/SimpleFillSymbol",
         "esri/Color", "esri/Graphic", "esri/views/2d/draw/Draw", "esri/geometry/Point"],
        function (SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, Color, Graphic, Draw, Point) {

            var draw = new Draw({
                view: view
            });

            var markerSyl = new SimpleMarkerSymbol({
                color: [0, 255, 100, 0.6],
                size: 10,
            });
            switch (geoType) {
                case "Point":
                    var action = draw.create("point");
                    action.on("cursor-update", function (evt) {
                        createPointGraphic(evt.coordinates);
                    });
                    action.on("draw-complete", function (evt) {
                        createPointGraphic(evt.coordinates);
                    }); 
                    break;
                case "Line":
                    var action = draw.create("polyline");
                    action.on("vertex-add", function (evt) {
                        createPolylineGraphic(evt.vertices);
                    });
                    action.on("vertex-remove", function (evt) {
                        createPolylineGraphic(evt.vertices);
                    });
                    action.on("cursor-update", function (evt) {
                        createPolylineGraphic(evt.vertices);
                    });
                    action.on("draw-complete", function (evt) {
                        createPolylineGraphic(evt.vertices);
                    }); 
                    break;
                case "Polygon":
                    var action = draw.create("polygon");
                    action.on("vertex-add", function (evt) {
                        createPolygonGraphic(evt.vertices);
                    });
                    action.on("vertex-remove", function (evt) {
                        createPolygonGraphic(evt.vertices);
                    });
                    action.on("cursor-update", function (evt) {
                        createPolygonGraphic(evt.vertices);
                    });
                    action.on("draw-complete", function (evt) {
                        createPolygonGraphic(evt.vertices);
                    });
                    break;
                case "Clean":
                    view.graphics.removeAll();
                    break;
            }

            function createPointGraphic(coordinates) {
                view.graphics.removeAll();
                var point = {
                    type: "point", // autocasts as /Point
                    x: coordinates[0],
                    y: coordinates[1],
                    spatialReference: view.spatialReference
                };

                var graphic = new Graphic({
                    geometry: point,
                    symbol: {
                        type: "simple-marker", // autocasts as SimpleMarkerSymbol
                        style: "circle",
                        color: [255,255,255,0.5],
                        size: "15px",
                        outline: { // autocasts as SimpleLineSymbol
                            color: [255, 255, 0],
                            width: 1
                        }
                    }
                });
                view.graphics.add(graphic);
            }

            function createPolylineGraphic(vertices) {
                view.graphics.removeAll();
                var polyline = {
                    type: "polyline", // autocasts as Polyline
                    paths: vertices,
                    spatialReference: view.spatialReference
                };

                var graphic = new Graphic({
                    geometry: polyline,
                    symbol: {
                        type: "simple-line", // autocasts as SimpleLineSymbol
                        color: [4, 90, 141,0.7],
                        width: 1,
                        cap: "round",
                        join: "round"
                    }
                });
                view.graphics.add(graphic);
            }

            function createPolygonGraphic(vertices) {
                view.graphics.removeAll();
                var polygon = {
                    type: "polygon", // autocasts as Polygon
                    rings: vertices,
                    spatialReference: view.spatialReference
                };

                var graphic = new Graphic({
                    geometry: polygon,
                    symbol: {
                        type: "simple-fill", // autocasts as SimpleFillSymbol
                        color: [0,255,100,0.5],
                        style: "solid",
                        outline: {  // autocasts as SimpleLineSymbol
                            color: "white",
                            width: 1.5
                        }
                    }
                });
                view.graphics.add(graphic);
            }
        });
};